package keye.document_manage.dao.sqlserver;

import java.util.HashMap;
import java.util.List;

import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;

import keye.document_manage.domain.ClassEntityRelation;

public interface IClassEntityRelationDao {
    int deleteByPrimaryKey(String id);

    int insert(ClassEntityRelation record);

    int insertSelective(ClassEntityRelation record);

    ClassEntityRelation selectByPrimaryKey(String id);

    int updateByPrimaryKeySelective(ClassEntityRelation record);

    int updateByPrimaryKey(ClassEntityRelation record);
    
    List<ClassEntityRelation> getAllClassEntityRelations();
    
    @Select("select * from class_entity_relation where p_id is null")
    @ResultMap(value = "BaseResultMap")
    List<ClassEntityRelation> getTreeRoot();
    
    @Select("select * from class_entity_relation where p_id = #{pid,jdbcType=VARCHAR}")
    @ResultMap(value = "BaseResultMap")
    List<ClassEntityRelation> getTreeNode(String pid);
    
    /**
     * 得到节点和其所有父节点
     * @param pid
     * @return
     */
    @Select(" with cte as  (  "
    		+ " select  a.id, a.p_id, a.item_name, a.item_type, a.c_e_type, a.e_table_name, a.item_order from class_entity_relation a where p_id=#{pid}"
    		+ " union all "
    		+ " select k.id, k.p_id, k.item_name, k.item_type, k.c_e_type, k.e_table_name, k.item_order  from class_entity_relation k inner join cte c on c.p_id = k.id  "
    		+ " )select * from cte  ")
    //@ResultMap(value = "BaseResultMap")
    List<HashMap<String, Object>> getAllParent(String pid);
}